package com.example.controller;


import com.example.domain.Category;
import com.example.domain.DataJson;
import com.example.domain.Result;
import com.example.domain.TableData;
import com.example.service.CategoryService;
import com.example.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @GetMapping("/toCategory")
    public String toSeek() {
        return "category/category";
    }


    @PostMapping("addCategory")
    @ResponseBody
    public DataJson addCategory(@RequestBody Category category) {
        System.out.println("controller" + category);
        categoryService.save(category);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

//    @PostMapping("updateCategory")
//    public Boolean updateCateGory(@RequestBody Category category){
//        return categoryService.updateById(category);
//    }

    @PostMapping("deleteCategory")
    @ResponseBody
    public DataJson deleteCategory(@RequestParam("id") Integer id) {
//        System.out.println("testd------>"+id);
        categoryService.removeById(id);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

    @PostMapping("batchDeleteCategory")
    @ResponseBody
    public DataJson batchDeleteCategory(@RequestParam("ids[]") Integer ids[]) {
        System.out.println("testd------>" + Arrays.toString(ids));

        categoryService.removeByIds(Arrays.asList(ids));
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        dataJson.setMsg("删除成功");
        return dataJson;
    }

    @GetMapping("getCategoryById/{id}")
    public Category getCategoryById(@PathVariable Integer id) {
        return categoryService.getById(id);
    }

    @GetMapping("/getPage")
    @ResponseBody
    public TableData getPage(Integer page, Integer limit, String cateName) {
        return categoryService.getPage(page, limit, cateName);
    }

    @RequestMapping("image")
    @ResponseBody
    public DataJson image(MultipartFile file) {
        //调用工具类完成文件上传
        String imagePath = UploadUtils.upload(file);

        System.out.println(imagePath);

        DataJson dataJson = new DataJson();
        if (imagePath != null) {
            //创建一个HashMap用来存放图片路径
            HashMap hashMap = new HashMap();
            hashMap.put("src", imagePath);
            dataJson.setCode(0);
            dataJson.setMsg("上传成功");
            dataJson.setData(hashMap);
        } else {
            dataJson.setCode(1);
            dataJson.setMsg("上传失败");
        }
        return dataJson;
    }


    @PostMapping("updateCategory")
    @ResponseBody
    public DataJson updateCategory(@RequestBody Category category) {
        //获得图片地址和图片描述
        /*
            这里我们只做打印的操作，实际上，我们应该在这里调用
            方法，把图片地址和图片的描述加入到数据库中，但是这些
            相信大家已经回了，所以，就不再写了。
         */
        System.out.println("图片描述：" + category.getCateIcon());

        return categoryService.updateCategory(category);

    }

//    @RequestMapping("deleteImage")
//    @ResponseBody
//    public String deleteImage(){
//        UploadUtils.delete("84e15dddb4284fc8a4877c93bcc9d81f-dl.jpg");
//        return "1";
//    }


    //微信端
    @GetMapping("getAllCategory")
    @ResponseBody
    public Result getAll() {
        Result result = new Result();

        return Result.success(categoryService.list(null));
    }

    @GetMapping("getAllCategoryName")
    @ResponseBody
    public Result getAllCategoryName() {
        Result result = new Result();

        List list = new ArrayList();
        for (Category category : categoryService.list(null)) {
            list.add(category.getCateName());
        }
        return Result.success(list);
    }
}
